<template>
  <view class="container">
    <tui-list-view title="解析源：">
      <tui-button type="primary" @click="addSource">+添加</tui-button>
      <tui-list-cell
        v-for="(source, index) in sources"
        :key="index"
        @click="goSource(source,index)"
      >
        <view style="width:200upx;">{{source.name}}</view>
        <view class="tui-flex-1">{{source.url}}</view>
        <view style="width:50upx;">
          <tui-icon v-if="source.enable" name="check" color="green" :size="50" :rpx="true" />
          <tui-icon v-else name="shut" color="red" :size="50" :rpx="true" />
        </view>
      </tui-list-cell>
    </tui-list-view>
    <tui-tips
      v-if="sources.length == 0"
      :position="center"
      imgUrl="/static/images/toast/img_nodata.png"
    >暂无解析源规则</tui-tips>
    <uni-pagination
      v-else
      :current="pageIndex + 1"
      :pageSize="pageSize"
      :total="total"
      @change="goPage"
    />
  </view>
</template>

<script>
import { mapState } from "vuex";
import tuiListView from "@/components/list-view/list-view.vue";
import tuiListCell from "@/components/list-cell/list-cell.vue";
import uniPagination from "@/components/uni-pagination/uni-pagination.vue";
import tuiButton from "@/components/button/button.vue";
import tuiIcon from "@/components/icon/icon.vue";
import tuiTips from "@/components/extend/tips/tips";

export default {
  components: {
    tuiListView,
    tuiListCell,
    uniPagination,
    tuiButton,
    tuiIcon,
    tuiTips
  },
  data() {
    return {
      sources: [],
      pageIndex: 0,
      pageSize: 10,
      total: 0
    };
  },
  computed: mapState(["config"]),
  onLoad(options) {
    if (options.refresh) {
      this.init();
    }
    if (!this.config.admin) {
      this.tui.toast("您不是管理员！");
      setTimeout(() => {
        uni.navigateBack({
          delta: 1
        });
      }, 1000);
      return;
    }
    this.init();
  },
  methods: {
    init() {
      uni.showLoading({
        title: "加载中"
      });
      this.db
        .collection("book_sources")
        .skip(this.pageIndex * this.pageSize)
        .limit(this.pageSize)
        .orderBy("enable", "desc")
        .get()
        .then(res => {
          // console.log("res", res);
          if (res.errMsg == "collection.get:ok") {
            this.sources = res.data;
            this.caclTotal();
          } else {
            uni.hideLoading();
            this.tui.toast(res.errMsg);
          }
        })
        .catch(e => {
          console.error(e);
          uni.hideLoading();
        });
    },
    caclTotal() {
      this.db
        .collection("book_sources")
        .count()
        .then(res => {
          uni.hideLoading();
          if (res.errMsg == "collection.count:ok") {
            this.total = res.total;
          } else {
            this.tui.toast(res.errMsg);
          }
        })
        .catch(e => {
          console.error(e);
          uni.hideLoading();
        });
    },
    goPage({ type, current }) {
      // console.log(type, current);
      this.pageIndex = current - 1;
      this.$nextTick(this.init);
    },
    addSource() {
      uni.navigateTo({
        url: "/pages/admin/source"
      });
    },
    goSource(source, index) {
      uni.setStorage({
        key: "source_edit",
        data: source,
        success: function() {
          uni.navigateTo({
            url: "/pages/admin/source"
          });
        }
      });
    }
  }
};
</script>

<style>
</style>
